package com.ht.pms.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ht.pms.core.dao.SalaryUserDao;
import com.ht.pms.core.dto.Result;
import com.ht.pms.core.dto.user.DdUser;
import com.ht.pms.core.dto.user.UcOrg;
import com.ht.pms.core.dto.user.UcUser;
import com.ht.pms.core.feign.UcFeign;
import com.ht.pms.core.service.SalaryUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class SalaryUserServiceImpl implements SalaryUserService {


    @Autowired
    private SalaryUserDao salaryUserDao;
    @Autowired
    private UcFeign ucFeign;


    @Override
    public void updateUserInfo(String startTime, String endTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.now();
        if (startTime == null) {
            startTime = salaryUserDao.queryEndTime();
            if (startTime == null) {
                startTime = "2017-04-19 00:00:00";
            }
        }
        if(endTime == null){
             endTime = df.format(ldt);
        }
        List<UcUser> ucUserList = ucFeign.getUserListByTime(startTime, endTime);
        if (!CollectionUtils.isEmpty(ucUserList)) {
            salaryUserDao.updateUserInfo(ucUserList);
        }
        salaryUserDao.saveQueryStartTime(startTime, endTime);
    }

    @Override
    public void updateDdUser(String startTime, String endTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.now();
        if (startTime == null) {
            startTime = salaryUserDao.queryEndTime();
            if (startTime == null) {
                startTime = "2017-04-19 00:00:00";
            }
        }
        if(endTime == null){
            endTime = df.format(ldt);
        }
        String result=ucFeign.getAllDDUser();
        JSONObject jsonObject= JSON.parseObject(result);

        if(jsonObject.getString("returnCode") .equals("0000") ){
//            List<DdUser> ddUserList = result.getData();
            String dddate=jsonObject.getString("data");
            List<DdUser> ddUserList= JSONArray.parseArray(dddate,DdUser.class);
            salaryUserDao.updateDdUser(ddUserList);
        }
        salaryUserDao.saveQueryStartTime(startTime, endTime);
    }

    @Override
    public void updateOrgInfo(String startTime, String endTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.now();
        if (startTime == null) {
            startTime = salaryUserDao.queryEndTime();
            if (startTime == null) {
                startTime = "2017-04-19 00:00:00";
            }
        }
        if(endTime == null){
            endTime = df.format(ldt);
        }
        List<UcOrg> ucOrgList=ucFeign.getOrgListByTime(startTime, endTime);
        if (!CollectionUtils.isEmpty(ucOrgList)) {
            salaryUserDao.updateOrgInfo(ucOrgList);
        }
        salaryUserDao.saveQueryStartTime(startTime, endTime);

    }
}
